package com.adam.example;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;

import java.net.UnknownHostException;

public class ClusterAdminExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        ClusterAdminClient clusterAdminClient = transportClient.admin().cluster();
        ClusterHealthResponse clusterHealthResponse = clusterAdminClient.prepareHealth().get();
        String clusterName = clusterHealthResponse.getClusterName();
        int numberOfDataNodes = clusterHealthResponse.getNumberOfDataNodes();
        int numberOfNodes = clusterHealthResponse.getNumberOfNodes();
        ClusterHealthStatus clusterHealthStatus = clusterHealthResponse.getStatus();
        System.out.println("集群名称" + clusterName + "，健康状态" + clusterHealthStatus + "，数据节点数" + numberOfDataNodes + "，节点数" + numberOfNodes);
        for(ClusterIndexHealth clusterIndexHealth: clusterHealthResponse.getIndices().values()) {
            String index = clusterIndexHealth.getIndex();
            int numberOfShards = clusterIndexHealth.getNumberOfShards();
            int numberOfReplicas = clusterIndexHealth.getNumberOfReplicas();
            ClusterHealthStatus indexHealthStatus = clusterIndexHealth.getStatus();
            System.out.println("索引" + index + "，分片数" + numberOfShards + "，副本数" + numberOfReplicas + "，状态" + indexHealthStatus);
        }

        transportClient.close();
    }

}
